/**
 * Copyright 2014,Peak Tai 台俊峰(taijunfeng_it@sina.com).
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * 		http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.cenluan.me.data;

import com.cenluan.common.Const;
import com.cenluan.ext.jfinal.PkController;
import com.cenluan.user.User;
import com.jfinal.aop.Before;

/**
 * 个人资料
 * 
 * @author peak
 * 
 */
public class MydataController extends PkController {
	/**
	 * 显示个人资料
	 */
	public void index() {

	}

	/**
	 * 更新
	 */
	@Before(MydataValidator.class)
	public void update() {
		User user = getSessionAttr(Const.SESSION_USER);
		User updateUser = getModel(User.class);
		updateUser.set("id", user.getStr("id"));
		updateUser.keep("id", "nickname", "signature").update();
		setSessionAttr(Const.SESSION_USER, User.dao.findById(user.getStr("id")));
		setAttr("ok", "true");
		renderJson();
	}

	/**
	 * 检查昵称
	 */
	public void checkName() {
		User user = getSessionAttr(Const.SESSION_USER);
		String nickname = getPara("user.nickname");
		if (user.getStr("nickname").equals(nickname)) {
			renderJson(true);
			return;
		}
		if (User.dao.findByNickname(nickname) != null) {
			renderJson(false);
			return;
		}
		renderJson(true);

	}

}
